package com.apy.module.network.model

import com.squareup.moshi.JsonClass

/**
 * A generic class that holds a value with its loading status.
 * @param T The type of data to be wrapped
 */
sealed class ApiResult<out T> {
    data class Success<out T>(val data: T) : ApiResult<T>()
    data class Error(val exception: Exception) : ApiResult<Nothing>()
    object Loading : ApiResult<Nothing>() 
    
    override fun toString(): String {
        return when (this) {
            is Success<*> -> "Success[data=$data]"
            is Error -> "Error[exception=${exception.message}]"
            Loading -> "Loading"
        }
    }
}

/**
 * Extension function to handle API result
 */
suspend fun <T> safeApiCall(apiCall: suspend () -> T): ApiResult<T> {
    return try {
        ApiResult.Success(apiCall.invoke())
    } catch (e: Exception) {
        ApiResult.Error(e)
    }
}

/**
 * Standard API response wrapper
 */
@JsonClass(generateAdapter = true)
data class ApiResponse<T>(
    val code: Int,
    val message: String,
    val data: T? = null
) {
    val isSuccessful: Boolean
        get() = code == 200 // Adjust based on your API's success code
}
